﻿//创建时间: 2011-1-24
//创建作者: 李晓忠
//功能说明: CMS系统 操作日志SQLServer操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TRPOP.Config;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using TRPOP.CMS.Plugin;
using System.Data;
using TRPOP.Common;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Log
{
    public class LogDALSqlServer
    {
        TRPOP.ComSqlDAL.ICommonDAL mICommonDAL = TRPOP.CMS.Plugin.ICommonDALAccess.GetInstance();

        #region Save
        public EnumSubmitResult SaveLog(int UserID, string UserName, string IP, string Action, string ActionURL, DateTime AddTime)
        {
            string strStoreProcedure = SystemInfoHelper.getSystemInfo().DBprefixTableName + "_" + "oper_log_Save";
            SqlDatabase odbDataBase = mICommonDAL.GetSqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetStoredProcCommand(strStoreProcedure);
            odbDataBase.AddInParameter(odbCommand, "@UserID", System.Data.DbType.Int32, UserID);
            odbDataBase.AddInParameter(odbCommand, "@UserName", System.Data.DbType.String, UserName);
            odbDataBase.AddInParameter(odbCommand, "@IP", System.Data.DbType.String, IP);
            odbDataBase.AddInParameter(odbCommand, "@Action", System.Data.DbType.String, Action);
            odbDataBase.AddInParameter(odbCommand, "@ActionURL", System.Data.DbType.String, ActionURL);
            odbDataBase.AddInParameter(odbCommand, "@AddTime", System.Data.DbType.DateTime, AddTime);
            odbDataBase.AddParameter(odbCommand, "@LogID", System.Data.DbType.Int32, ParameterDirection.InputOutput, null, DataRowVersion.Default, 0);
            return mICommonDAL.ExeByCommandSqlDatabase(odbDataBase, odbCommand);
        }
        #endregion

        #region DeleteInfoById
        public EnumSubmitResult DeleteInfoById(int id)
        {
           return  DeleteInfoByIds(id.ToString());
        }
        #endregion

        #region DeleteInfoByIds
        public EnumSubmitResult DeleteInfoByIds(string ids)
        {
            string strSqlString = "DELETE FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_oper_log WHERE [LogID] in(" + ids + ")";
            return mICommonDAL.ExeSQLQuery(strSqlString, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
        }
        #endregion

        #region GetListByCondition
        public System.Data.DataSet GetListByCondition(int topn, string condition, string orderby)
        {
            string strSqlString = "SELECT ";
            if (topn > 0)
            {
                strSqlString += " TOP " + topn;
            }
            strSqlString += " * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_oper_log WHERE 1=1"; if (condition != "")
            {
                strSqlString += condition;
            }
            if (orderby != "")
            {
                strSqlString += " ORDER BY " + orderby;
            }
            else
            {
                strSqlString += " ORDER BY LogID DESC";
            }
            return mICommonDAL.GetDataSet(strSqlString, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
        }
        #endregion

        #region GetListByPager
        public System.Data.DataSet GetListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            //string strStoreProcedure = SystemInfoHelper.getSystemInfo().PagerStoreProcedureName;
            //SqlDatabase odbDataBase = mICommonDAL.GetSqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            //DbCommand odbCommand = odbDataBase.GetStoredProcCommand(strStoreProcedure);

            //odbDataBase.AddInParameter(odbCommand, "@TableName", System.Data.DbType.String, SystemInfoHelper.getSystemInfo().DBprefixTableName + "_oper_log");
            //odbDataBase.AddInParameter(odbCommand, "@ShowColumns", System.Data.DbType.String, "*");
            //odbDataBase.AddInParameter(odbCommand, "@pageNumber", System.Data.DbType.Int32, pageNumber);
            //odbDataBase.AddInParameter(odbCommand, "@pageSize", System.Data.DbType.Int32, pageSize);
            //odbDataBase.AddInParameter(odbCommand, "@condition", System.Data.DbType.String, condition);
            //odbDataBase.AddInParameter(odbCommand, "@orderby", System.Data.DbType.String, orderby);
            //odbDataBase.AddOutParameter(odbCommand, "@recordCount", System.Data.DbType.Int32, 0);
            //recordCount = 0;
            //return mICommonDAL.GetDataSet(odbDataBase, odbCommand, out recordCount, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            return mICommonDAL.GetListByPager(SystemInfoHelper.getSystemInfo().DBprefixTableName + "_oper_log", "*", pageNumber, pageSize, condition, orderby, out recordCount, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
        }
        #endregion
    }
}